package com.wen.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.yygh.common.result.Result;
import com.wen.yygh.common.utils.MD5;
import com.wen.yygh.hosp.service.HospitalSetService;
import com.wen.yygh.model.hosp.HospitalSet;
import com.wen.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author Wenjl
 */

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Slf4j
@Api(tags = "医院接口设置")
public class HospitalSetController {
    /**
     * 注入service
     */
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置表所有信息
     *
     * @return
     */
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();

        return Result.ok(list);
    }

    /**
     * 删除医院设置
     *
     * @param id 根据id删除
     * @return
     */
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospitalSet(@PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);

        return b ? Result.ok() : Result.fail();
    }

    /**
     * 按照条件分页获取医院设置
     *
     * @param pageNum            页码
     * @param pageSize           页面大小
     * @param hospitalSetQueryVo 查询条件
     * @return
     */
    @ApiOperation(value = "按照条件分页获取医院设置")
    @PostMapping("findPageHospSet/{pageNum}/{pageSize}")
    public Result findPageHospitalSet(@PathVariable("pageNum") Integer pageNum,
                                      @PathVariable("pageSize") Integer pageSize,
                                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建分页对象，传递当前页和页大小
        Page<HospitalSet> page = new Page<>(pageNum, pageSize);

        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        //构造条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hosname)) {

            queryWrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())) {
            queryWrapper.eq("hoscode", hoscode);
        }
        //调用service层方法实现分页待条件查询
        return Result.ok(hospitalSetService.page(page, queryWrapper));

    }

    /**
     * 添加医院设置
     *
     * @param hospitalSet 参数
     * @return
     */
    @ApiOperation("添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1正常使用 0不能使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        return save ? Result.ok() : Result.fail();

    }

    /**
     * 根据id获取医院设置
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取医院设置")
    @GetMapping("getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        return Result.ok(hospitalSet);
    }

    /**
     * 根据id更新医院设置
     *
     * @param hospitalSet 参数
     * @return
     */
    @ApiOperation("根据id更新医院设置")
    @PostMapping("updateHospitalSetById")
    public Result updateHospitalSetById(@RequestBody HospitalSet hospitalSet) {
        boolean result = hospitalSetService.updateById(hospitalSet);
        return result ? Result.ok() : Result.fail();
    }

    /**
     * 根据id批量逻辑删除医院设置
     *
     * @param idList id集合
     * @return
     */
    @ApiOperation("根据id批量逻辑删除医院设置")
    @DeleteMapping("batchRemoveHospitalSet")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean result = hospitalSetService.removeByIds(idList);
        return result ? Result.ok() : Result.fail();
    }

    /**
     * 医院设置锁定和解锁
     *
     * @param id
     * @param status 状态
     * @return
     */
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status) {
        //根据id获取医院设置
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //修改状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    /**
     * 发送医院密钥
     *
     * @param id
     * @return
     */
    @ApiOperation("发送医院密钥")
    @GetMapping("sendKey/{id}")
    public Result sendKey(@PathVariable("id") Long id) {
        //根据id获取医院设置
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //密钥
        String signKey = hospitalSet.getSignKey();
        //医院代码
        String hoscode = hospitalSet.getHoscode();

        return Result.ok();
    }
}
